package Akanksha;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Class to create Basal Profiles
 * @author Stephen
 */
public class BasalProfileFactory {
    ///Array of basal profiles
    public BasalProfile[] userBasalProfiles;
    
    public BasalProfileFactory(){
        userBasalProfiles = new BasalProfile[3];
    }
    
    ///Use to save the basal profile
    public void saveBasalProfiles() {
        try {
//            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("BasalProfiles.txt", true))); //append to file
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("BasalProfiles.txt", false))); //overwrite file
            for(int iter = 0; iter < 24; iter++)
            {
                out.println( userBasalProfiles[0].profile[iter] + "," + userBasalProfiles[1].profile[iter] + "," + userBasalProfiles[2].profile[iter] );
            }
            out.close();
        }   
            catch (IOException e) {
            System.out.println("ERROR:  Exception:  " + e.toString());
        }
    }
    
    public void readBasalProfiles() {
        try {
            BufferedReader CSVFile = new BufferedReader(new FileReader("BasalProfiles.txt"));

            String dataRow = CSVFile.readLine();
            int rowIter = 0;
            while(dataRow != null){
                String[] dataArray = dataRow.split(",");
                int profileIter = 0;
                for(String item:dataArray){
                    userBasalProfiles[profileIter].profile[rowIter] = Float.parseFloat(item);
                    profileIter++;
                }
                dataRow = CSVFile.readLine();
                rowIter++;
            }
            CSVFile.close();
        } catch (IOException e) {
            System.out.println("ERROR:  Exception:  " + e.toString());
        }
    }
    
    ///Used for testing this class
//     public static void main (String args[]) {
//         BasalProfileFactory profileFactory = new BasalProfileFactory();
//        
//         BasalProfile profile1 = new BasalProfile();
//         for(int iter = 0; iter < 24; iter++)
//         {
//            profile1.profile[iter] = iter + 1;
//         }
//         profileFactory.userBasalProfiles[0] = profile1;
//        
//         BasalProfile profile2 = new BasalProfile();
//         for(int iter = 0; iter < 24; iter++)
//         {
//            profile2.profile[iter] = profile1.profile[iter] + 1;
//         }
//         profileFactory.userBasalProfiles[1] = profile2;
//        
//         BasalProfile profile3 = new BasalProfile();
//         for(int iter = 0; iter < 24; iter++)
//         {
//            profile3.profile[iter] = profile2.profile[iter] + 1;
//         }
//         profileFactory.userBasalProfiles[2] = profile3;
//        
//         profileFactory.saveBasalProfiles();
//         profileFactory.readBasalProfiles();
         
         //Display the profiles
//         System.out.println("Profile 1");
//         profileFactory.userBasalProfiles[0].display();
//         System.out.println("Profile 2");
//         profileFactory.userBasalProfiles[1].display();
//         System.out.println("Profile 3");
//         profileFactory.userBasalProfiles[2].display();
//     }    
}
